package sr.codemg.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sr.codemg.entity.SrModulecfg;
import sr.codemg.mapper.SrModulecfgMapper;
import sr.codemg.service.SrModulecfgService;
import sr.codemg.utils.Constants;
import sr.codemg.utils.ResponseCode;
import sr.codemg.utils.StaticFunc;
import sr.codemg.vm.Base;
import sr.codemg.vm.Selects;
import sr.codemg.vm.Tables;

import java.util.List;
import java.util.Map;

/**
 * 模块配置
 **/
@Service
public class SrModulecfgServiceImpl implements SrModulecfgService {

    @Autowired
    SrModulecfgMapper srModulecfgMapper;

    @Override
    public SrModulecfg get(String id) {
        return srModulecfgMapper.get(id);
    }

    @Override
    public Tables findList(Map<String, Object> params) {
        StaticFunc.page(params);
        List<SrModulecfg> data = srModulecfgMapper.findList(params);
        Integer count = srModulecfgMapper.count(params);
        return new Tables(data, count);
    }

    @Override
    public Base.Response add(SrModulecfg entity) {
        srModulecfgMapper.add(entity);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Base.Response update(SrModulecfg entity) {
        SrModulecfg tmp = new SrModulecfg();
        tmp.setEnName(entity.getEnName());
        List<SrModulecfg> list = srModulecfgMapper.findByFields(tmp);
        if (list.size() > 0) {
            srModulecfgMapper.update(entity);
        } else {
            entity.setId("");
            this.add(entity);
        }
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Base.Response delete(String id) {
        srModulecfgMapper.delete(id);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Selects.Response select(Selects.Request req) {
        req.configPage();
        List<Selects.Select> data = srModulecfgMapper.select(req);
        Selects.Response res = new Selects.Response();
        res.setData(data);
        return res;
    }
}

